package com.xzx.taskqueue;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

import java.nio.charset.StandardCharsets;

/**
 *
 * @author xinzhixuan
 * @version 1.0
 * @date 2025/10/24 16:42
 */
public class ClientHandler extends ChannelInboundHandlerAdapter  {

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        System.out.println("客户端成功建立连接");
        ByteBuf byteBuf = Unpooled.copiedBuffer("hello 服务端".getBytes(StandardCharsets.UTF_8));
        ctx.writeAndFlush(byteBuf);
    }

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        ByteBuf buf = (ByteBuf) msg;
        System.out.println("服务端返回的数据：" + buf.toString(StandardCharsets.UTF_8));
        System.out.println(Thread.currentThread().getName());
        ctx.channel().eventLoop().execute(new Runnable() {

            @Override
            public void run() {
                System.out.println("执行任务" + Thread.currentThread().getName());
            }
        });
    }
}
